package me.xhy.java.springboot3.r2dbc.s03databaseclient;

import me.xhy.java.springboot3.r2dbc.s00application.S001Application;
import me.xhy.java.springboot3.r2dbc.s00application.S002Entity;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.r2dbc.core.DatabaseClient;

import java.util.concurrent.TimeUnit;

@SpringBootTest(classes = S001Application.class)
public class S031DatabaseClient {

  @Autowired
  private DatabaseClient databaseClient;

  @Test
  void test() throws Exception {
    System.out.println(databaseClient);
    // 更底层，直接写SQL，如 join 等操作就比较方便
    databaseClient
        .sql("select * from xs1_t_demo1 where dept_id = ?")
        .bind(0, 1)
        .fetch()
        .all()
        .map( i -> {
          System.out.println("=== map1 s===");
          System.out.println(i);
          System.out.println("=== map1 e===");
          return i;
        })
        .map(i -> {
          System.out.println("=== map2 s===");
          System.out.println(i);
          System.out.println("=== map2 e===");
          return new S002Entity(String.valueOf(i.get("id")), String.valueOf(i.get("dept_id")));
        }).subscribe(System.out::println);

    TimeUnit.SECONDS.sleep(10);

  }




}
